import wx
import gdata.docs.data
import gdata.docs.client
import gdata.data

def GetfileType(filename):

	filetype = filename.split('.')[-1]
	if filetype == 'doc':
		filetype = 'application/msword'
	elif filetype == 'docx':
		filetype = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
	elif filetype == 'ods':
		filetype = 'application/x-vnd.oasis.opendocument.spreadsheet'
	elif filetype == 'odt':
		filetype = 'application/vnd.oasis.opendocument.text'
	elif filetype == 'pdf':
		filetype = 'application/pdf'
	elif filetype == 'ppt':
		filetype = 'application/vnd.ms-powerpoint'
	elif filetype == 'pps':
		filetype = 'application/vnd.ms-powerpoint'
	elif filetype == 'txt':
		filetype = 'text/plain'
	elif filetype == 'rtf':
		filetype = 'application/rtf'
	elif filetype == 'html':
		filetype = 'text/html'
	elif filetype == 'xls':
		filetype = 'application/vnd.ms-excel'
	elif filetype == 'xlsx':
		filetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
	elif filetype == 'pps':
		filetype = 'application/vnd.ms-powerpoint'
	return filetype
	
def upload(filepath):

	#authorite
	client= gdata.docs.client.DocsClient(source='leo-easyupload-v1')
	client.ssl=True
	client.http_client.debug=False
	
	client.ClientLogin('leoyfm@gmail.com', 'MAYIFANleo85', client.source)
	
	filename = filepath.split('\\')[-1]
	filetype = GetfileType(filename)
	path = '\\'.join(filepath.split('\\'))
	
	print path, filename, filetype
	entry = client.Upload(path, filename, content_type=filetype)

#define file drop target class
class FileDropTarget(wx.FileDropTarget):
	#this object implements drop target function
	def __init__(self, obj):
		wx.FileDropTarget.__init__(self)
		self.obj = obj
	
	def OnDropFiles(self, x,y, filenames):
		#implement file drop
		self.obj.SetInsertionPointEnd()
		self.obj.WriteText("%d file(s) dropped:\n" %(len(filenames)))
		for file in filenames:
			self.obj.WriteText(file + '\n')
			upload(file)

class MainWindow(wx.Frame):
	def __init__(self, parent,id,title):
		wx.Frame.__init__(self,parent,-4, title, size = (200,200),style=wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN )
		self.SetBackgroundColour(wx.WHITE)
		self.SetMaxSize(wx.Size(200,200))
		self.SetMinSize(wx.Size(200,200))
#		menuBar = wx.MenuBar()
#		menu1 = wx.Menu()
#		menu1.Append(wx.ID_EXIT, "E&xit", "Quit Application")
#		menuBar.Append(menu1, "&File")
#		self.SetMenuBar(menuBar)
#		wx.EVT_MENU(self, wx.ID_EXIT, self.CloseWindow)

		self.text=wx.TextCtrl(self,-1,"", size=(80,80),style=wx.TE_MULTILINE|wx.TE_READONLY)

		dt=FileDropTarget(self.text)
		self.text.SetDropTarget(dt)
		self.Show(True)

	def CloseWindow(self, event):
		self.Close()
	
	def OnDragInit(self, event):
		tdo = wx.PyTextDataObject(self.text.GetStringSelection())
		tds = wx.DropSource(self.text)
		tds.SetData(tdo)
		tds.DoDragDrop(True)

	
class MyApp(wx.App):
	def OnInit(self):
		frame = MainWindow(None, -1, "Drag and Drop")
		self.SetTopWindow(frame)
		return True
if __name__ == '__main__':
	import sys
	print sys.argv
	app = MyApp(0)
	app.MainLoop()



